<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/20
 * Time: 13:19
 */

namespace App\Http\Controllers\Api;

use App\Handlers\RedisHandler;
use App\Helpers\Api\ApiResponse;
use App\Http\Controllers\Controller;
use App\Models\Account;
use App\Models\Device;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\Response as FoundationResponse;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;

class NewGetController extends ApiController
{
    /**
     * @apiPermission none
     * @apiPermission anyone
     * @apiGroup Api
     * @api {get} /api/v1/getApiAccount 获取一个账号
     * @apiName getApiAccount
     * @apiVersion 1.0.0
     * @apiDescription app 获取一个账号
     * @apiParam {String} machine 机器de码
     * @apiParam {int} status   状态码
     * @apiSuccess {json} Success-Response:
     * { "result": "测试" ,"status_code":0}
     * @apiSampleRequest /api/v1/getApiAccount
     */
    public function getApiAccount(Request $request)
    {
        try {
            $redis = app("redis");
            $status =$request->get("status");

            $len=$redis->llen("account:list:".$status);
            //如果个数小于3000
            if($redis->llen("account:list:".$status)<3000){
                RedisHandler::syncApiAccount($len,$status);
            }
            if($len <=0){
                return $this->error("合适的账号不存在");
            }else{
                $accounts =$redis->lpop("account:list:".$status);
            }
            $key = "msg:account:".$accounts;
            $account = $redis->hgetall($key);
            $data = [
                //设备相关
                'email' => $account['email'],
                'cert' => $account['cert'],
                'password' => $account['password'],
                'Record_at' => $account['Record_at'],
            ];
            if (count($data)==0){
                return $this->error("合适的账号不存在");
            }
            return $this->success($data);
        } catch (\Exception $e) {
            return $this -> error($e ->getMessage());
        }
    }
    /**
     * @apiPermission none
     * @apiPermission anyone
     * @apiGroup Api
     * @api {post} /api/v1/replyApiAccount 回执账号
     * @apiName replyApiAccount
     * @apiVersion 1.0.0
     * @apiParam {String} email 账号
     * @apiParam {String} cert  证书码
     * @apiParam {String} status  状态
     * @apiParam {int} is   是否需要记录时间
     * @apiSuccess {json} Success-Response:
     * { "result": "测试" ,"status_code":0}
     * @apiSampleRequest /api/v1/replyApiAccount
     */
    public function replyApiAccount(Request $request){
        try {
            $redis = app("redis");
            $email =$request->get("email");
            $status =$request->get("status");
            $key = "msg:account:".$email;
            $account = $redis->hgetall("msg:account:".$email);
            if(!$account){
                return $this->error("账号不存在");
            }
                $redis->hset($key,"cert",$request->get("cert"));
                $redis->hset($key,"status",$status);
                if($request->get("is") ==1){
                    $redis->hset($key,"Record_at",time());
                }
                $redis->hset($key,"updated_at",Carbon::now());
                $data = [
                    'message' => '回执成功',
                ];
                //加入列表中
                $key ="account:list:".$status;
                $redis->rpush($key,$email);
                $redis->rpush("reply:account",$email);
            return $this->success($data);
        } catch (\Exception $e) {
            return $this -> error($e ->getMessage());

        }
    }
    /**
     * @apiPermission none
     * @apiPermission anyone
     * @apiGroup Api
     * @api {get} /api/v1/getApiDevice 1.获取一个设备
     * @apiName getApiDevice
     * @apiVersion 1.0.0
     * @apiDescription app 获取一个设备
     * @apiParam {String} machines 机器码
     * @apiParam {String} ProductType 设备型号
     * @apiParam {int} status 设备状态
     * @apiSuccess {json} Success-Response:
     * { "result": "测试" ,"status_code":0}
     * @apiSampleRequest /api/v1/getApiDevice
     */
    public function getApiDevice(Request $request)
    {
        try {
            $redis = app("redis");
            $tp =$request->get("ProductType");
            $status =$request->get("status");
            $key="device:list:".$status.':'.$tp;
            $len=$redis->llen($key);

            //如果个数小于3000
            if($len<3000){
                RedisHandler::syncApiDevice($len,$status);
            }
//            var_dump("wait");exit;
            if($len <=0){
                return $this->error("合适的账号不存在");
            }else{
                $device =$redis->lpop($key);
            }
            $code = $redis->hgetall("msg:device:".$device);
            $data = [
                //设备相关
                'udid' => $code['udid'],
                'BluetoothAddress' => $code['bt'],
                'Imei' => $code['imei'],
                'WiFiAddress' => $code['wifu'],
                'ecid' => $code['ecid'],
                'ProductType' => $code['tp'],
                'ModelNumber' => $code['nb'],
                'RegionInfo' => $code['reg'],
                'serial' => $code['sn'],
                'Ethernet' => $code['Ethernet'],
                'ICCID' => $code['ICCID'],
//                'IMSI' => $code['IMSI'],
//                'BasebandSerialNumber' => $code['BasebandSerialNumber'],
//                'BasebandMasterKeyHash' => $code['BasebandMasterKeyHash'],
//                'BasebandChipID' => $code['BasebandChipID'],
//                'Record_at' => $code['Record_at'],
            ];
            if (count($data)==0){
                return $this->error("合适的账号不存在");
            }
            return $this->success($data);
        } catch (\Exception $e) {

            return $this -> error($e ->getMessage());
        }
    }
    /**
     * @apiPermission none
     * @apiPermission anyone
     * @apiGroup Api
     * @api {get} /api/v1/replyApiDevice 2.回执设备
     * @apiName replyApiDevice
     * @apiVersion 1.0.0
     * @apiDescription app 回执设备
     * @apiParam {String} serial 设备serial
     * @apiParam {int} status  状态
     * @apiParam {int} is  是否需要记录时间1 记录
     * @apiParam {String} ProductType 设备型号
     * @apiSuccess {json} Success-Response:
     * { "result": "测试" ,"status_code":0}
     * @apiSampleRequest /api/v1/replyApiDevice
     */
    public function replyApiDevice(Request $request){
        try {
            $redis = app("redis");
            $sn =$request->get("serial");
            $tp =$request->get("ProductType");
            $status =$request->get("status");
            $key ="msg:device:".$sn;
            $code =$redis->hgetall($key);
            if ($code) {
                $data = [
                    'message' => '回执成功',
                ];
                $redis->hset($key,"status",$status);
                $redis->hset($key,"updated_at",Carbon::now());
                if($request->get("is") ==1){
                    $redis->hset($key,"Record_at",time());
                }
                $key ="device:list:".$status.":".$tp;
                $redis->rpush($key,$sn);

                $redis->rpush("reply:device",$sn);
                return $this->success($data);
            }
            return $this->error("合适的设备不存在");
        } catch (\Exception $e) {
            return $this -> error($e ->getMessage());
        }
    }
}